﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data.OleDb;


/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{

    public WebService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public int vratiNovoSaldo()
    {

        localhost.Servis_Grad s1 = new localhost.Servis_Grad();
        localhost.ServiceNimic s2 = new localhost.ServiceNimic(); //dodadi referenca
        localhost.Banka s3 = new localhost.Banka();
        

        string conString = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;

        OleDbConnection conn = new OleDbConnection(conString);

        conn.Open();

        string select = "SELECT grad FROM Table1";


        OleDbCommand cmd = new OleDbCommand(select, conn);

        OleDbDataReader citaj = cmd.ExecuteReader();

        string ime_grad = "";

        int cena = 0;

        while (citaj.Read())
        {
            string pom = citaj["grad"].ToString();

            if (s1.proveriGrad(pom) == 1)
            {
                ime_grad = pom;

                break;
            }
        }

        string select1 = "SELECT cena FROM Table1 where grad=" + ime_grad;

        OleDbCommand cmd1 = new OleDbCommand(select1, conn);

        OleDbDataReader citaj1 = cmd1.ExecuteReader();

        int cena_za_najden_grad = (int)citaj1["cena"];// do tuka najdovme grad i cena sega od servisot na nino zemi ime na igrac

        string ime_igrac = s2.getActivePlayer(); //go zemas imeto na igracot

        int balans = s3.showBalance(ime_igrac);

        if (balans>=cena_za_najden_grad)
        {
            return balans-=cena_za_najden_grad;
        }
        else
        {
           return balans;
        }

        conn.Close();
    }

}
